package com.zz.ecommerce.declareManage.exportB2CDeclare.cancelBillManage;

import com.zz.ecommerce.common.GridRequest;
import com.zz.ecommerce.common.GridResult;
import com.zz.ecommerce.common.constants.TableConstants;
import com.zz.ecommerce.declareManage.common.dao.ECancelMaterialsDao;
import com.zz.ecommerce.pojo.TECancelMaterials;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.UUID;

/**
 * Created by steven on 2016/3/30.
 */
@Controller
@RequestMapping("/exportB2C/cancelBillMaterials")
public class ECancelMaterialsController {

    @Autowired
    private ECancelMaterialsDao cancelMaterialsDao;

    /**
     * 查询核销单关联的料件信息列表
     *
     * @param relateId 关联核销单唯一索引
     * @param request
     * @return
     */
    @RequestMapping("/queryMaterialsList")
    @ResponseBody
    public GridResult queryMaterialsList(@RequestParam(value = "relateId") String relateId, GridRequest request) {
        if(StringUtils.isEmpty(relateId)){
            return new GridResult<>(null, request, 0);
        }

        TECancelMaterials query = new TECancelMaterials();
        query.setRelateId(relateId);
        long count = cancelMaterialsDao.count(TableConstants.T_E_CANCEL_MATERIALS, query);
        List<TECancelMaterials> list = cancelMaterialsDao.page(TableConstants.T_E_CANCEL_MATERIALS, query, request.getStart(),
                request.getEnd(), request.getSortOrder());

        return new GridResult<>(list, request, count);
    }

    /**
     * 删除核销单关联的料件信息
     *
     * @param id 料件信息唯一索引
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public boolean deleteMaterials(@RequestParam(value = "id") String id) {
        int count = cancelMaterialsDao.delete(TableConstants.T_E_CANCEL_MATERIALS, " WHERE ID = '" + id + "'");
        return count > 0;
    }

    /**
     * 新增核销单关联的料件信息
     *
     * @param materials 料件信息
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public boolean saveMaterials(TECancelMaterials materials) {
        int count;
        if(StringUtils.isNotEmpty(materials.getId())){
            TECancelMaterials query = new TECancelMaterials();
            query.setId(materials.getId());
            TECancelMaterials materials1 = cancelMaterialsDao.query(TableConstants.T_E_CANCEL_MATERIALS, query);
            if(null != materials1){
                count = cancelMaterialsDao.update(TableConstants.T_E_CANCEL_MATERIALS,materials," WHERE ID = #{updateObj.id}");
            }else{
                count = cancelMaterialsDao.insert(TableConstants.T_E_CANCEL_MATERIALS,materials);
            }
        }else{
            materials.setId(UUID.randomUUID().toString());
            count = cancelMaterialsDao.insert(TableConstants.T_E_CANCEL_MATERIALS,materials);
        }

        return count > 0;
    }
}
